﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Serialization;
using UnityEngine.UIElements;

public class ChessBoardMgr : SingletonMono<ChessBoardMgr>
{
    public GameObject latticeObj;

    public List<Vector2Int> clickListeners = new();
    
    public readonly Dictionary<Vector2Int, Lattice> ChessboardDict = new();
    
    private Lattice targetLattice;

    private void OnEnable()
    {
        base.Awake();
        GameObject chessBoard = new GameObject("ChessBoard");
        SceneManager.MoveGameObjectToScene(chessBoard, SceneManager.GetSceneByName("Battle"));
        GameObject go;
        Lattice l;
        for(float i = 0; i < 8; i++)
        {
            for(float j = 0; j < 8; j++)
            {
                Vector3 pos3 = new Vector3(i - 3.5f, j - 3.5f, 0);
                Vector2Int pos2 = new Vector2Int((int)i, (int)j);
                go = Instantiate(latticeObj, pos3, Quaternion.identity,chessBoard.transform);
                l = go.GetComponent<Lattice>();
                l.position = pos2;
                //Vector2ToVector3S.Add(pos2, pos3);
                ChessboardDict.Add(new Vector2Int((int)i, (int)j),l);
            }
        }
        
        EventCenter.GetInstance().AddEventListener("ClearTargetLattice",SpotlightIsNull);
        //EventCenter.GetInstance().AddEventListener<Lattice>("ClearTargetLattice",SpotlightIsChanged);
        EventCenter.GetInstance().AddEventListener<Lattice,Vector2IntMode,Color>(nameof(ShowTargetLattice),ShowTargetLattice);
        EventCenter.GetInstance().AddEventListener<Lattice,Color,Color>(nameof(ShowAttackTargetLattice),ShowAttackTargetLattice);
        EventCenter.GetInstance().AddEventListener<Vector2Int>(nameof(DisCardChess),DisCardChess);
    }

    private void OnDisable()
    {
        EventCenter.GetInstance().RemoveEventListener("ClearTargetLattice",SpotlightIsNull);
        //EventCenter.GetInstance().RemoveEventListener<Lattice>("ClearTargetLattice",SpotlightIsChanged);
        EventCenter.GetInstance().RemoveEventListener<Lattice,Vector2IntMode,Color>(nameof(ShowTargetLattice),ShowTargetLattice);
        EventCenter.GetInstance().RemoveEventListener<Lattice,Color,Color>(nameof(ShowAttackTargetLattice),ShowAttackTargetLattice);
        EventCenter.GetInstance().RemoveEventListener<Vector2Int>(nameof(DisCardChess),DisCardChess);
    }

    public void GameOver()
    {
        foreach(var value in ChessboardDict.Values)
        {
            value.chessDate = null;
            value.chess = null;
        }
    }

    public void DisCardChess(Vector2Int position)
    {
        ChessboardDict[position].chessDate = null;
        ChessboardDict[position].chess = null;
    }

    private void ShowTargetLattice(Lattice lattice,Vector2IntMode range,Color color)
    {
        Lattice l;
        foreach (var position in range.rangeType == RangeType.Normal ? 
                     Math.GetTargetPosition(lattice.position,range.vector2IntMode) : range.vector2IntMode)
        {
            l = ChessboardDict[position];
            if (l.chessDate) continue;
            l.AddClickListener();
            l.SetLatticeColor(color);
            clickListeners.Add(position);
        }
    }
    
    private void ShowAttackTargetLattice(Lattice lattice, Color attackColor,Color rangeColor)
    {
        Lattice l;
        var range = lattice.chessDate.attackRange;
        foreach (var position in range.rangeType == RangeType.Normal ? 
                     Math.GetTargetPosition(lattice.position,range.vector2IntMode) : range.vector2IntMode)
        {
            l= ChessboardDict[position];
            //if (l.chess.side == lattice.chess.side) continue;
            
            if (l.chess&&l.chess.GetChessSide() != lattice.chess.GetChessSide())
            {
                l.SetLatticeColor(attackColor);
                l.AddClickListener();
                clickListeners.Add(position);
            }
            if(l.chessDate)
                continue;
            l.SetLatticeColor(rangeColor);
            clickListeners.Add(position);
        }
    }
    
    private void SpotlightIsNull()
    {
        foreach (var position in clickListeners)
        {
            ChessboardDict[position].RemoveClickListener();
            ChessboardDict[position].SetLatticeColor(Settings.clear);
        }
        clickListeners.Clear();
    }
    

    
    public void SetChessDate(BaseChessDateSo date,Chess chess,Vector2Int position)
    {
        ChessboardDict[position].SetChessDate(date,chess);
    }
    

    public void MoveChess(Vector2Int originPosition, Vector2Int targetPosition)
    {
        ChessboardDict[targetPosition].SetChessDate
            (ChessboardDict[originPosition].chessDate,ChessboardDict[originPosition].chess);
        ChessboardDict[originPosition].chessDate = null;
        ChessboardDict[originPosition].chess = null;
    }
}


